package com.rede.didiok.admin.support;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.TypeReference;
import com.rede.didiok.admin.entity.AdminEntity;
import com.rede.didiok.admin.service.AdminService;
import com.rede.didiok.common.constant.conf.MessageConf;
import com.rede.didiok.common.domainEvent.EventAction;
import com.rede.didiok.common.dto.user.GeneralEditDto;
import com.rede.didiok.common.enums.ResourceTypeEnum;
import com.rede.didiok.common.exception.exceptionType.FeignException;
import com.rede.didiok.common.feign.RankFeignClient;
import com.rede.didiok.common.feign.UserFeignClient;
import com.rede.didiok.common.holder.RequestHolder;
import com.rede.didiok.common.utils.R;
import com.rede.didiok.common.utils.ResultUtil;
import com.rede.didiok.common.utils.business.DomainEventUtil;
import com.rede.didiok.common.utils.business.UserUtil;


@Service("generalEditSupport")
public class GeneralEditSupport {

    @Resource
    UserUtil userUtil;
    @Resource
    UserFeignClient userFeignClient;
    @Resource
    RankFeignClient rankFeignClient;
    @Resource
    AdminService adminService;
    @Resource
    private DomainEventUtil domainEventUtil;


    /**
     * 审批修改
     *
     * @param generalEditDto
     * @return
     */
    public String auditGeneralEdit(GeneralEditDto generalEditDto) {
        boolean flag;
        R generalEditResult = userFeignClient.getGeneralEditById(generalEditDto.getUid());
        GeneralEditDto generalEdit = generalEditResult.getData(new TypeReference<GeneralEditDto>(){});
        //判断审批的资源类型
        if (ResourceTypeEnum.RANK_MEMBER.getType().equals(generalEditDto.getBizType())) {
            //判断是榜单成员
            // 修改榜单成员content
            R updateResult = rankFeignClient.updateRankMemberContent(generalEditDto);
            if (updateResult.getCode() == 0) {
                flag = updateResult.getData(new TypeReference<Boolean>(){});
            }else {
                throw new FeignException("feign调用异常(修改rankMember的content失败)");
            }

        }  else {
            return ResultUtil.errorWithMessage("审核的资源不存在");
        }

        if (flag) {
            AdminEntity admin = adminService.getById(RequestHolder.getAdminUid());
            generalEdit.setAuditStatus(generalEditDto.getAuditStatus());
            generalEdit.setRejectReason(generalEditDto.getRejectReason());
            generalEdit.setAuditName(admin.getNickname());
            R updateResult = userFeignClient.updateGeneralEditById(generalEdit);
            boolean isSave = updateResult.getData(new TypeReference<Boolean>(){});
            if (isSave) {
                // 发送删除勘误事件
                GeneralEditDto dto = new GeneralEditDto();
                BeanUtils.copyProperties(generalEdit, dto);
                domainEventUtil.publishEvent(EventAction.GENERAL_EDIT_AUDIT, dto);
            }
            return ResultUtil.successWithMessage(MessageConf.OPERATION_SUCCESS);
        } else {
            return ResultUtil.errorWithMessage(MessageConf.OPERATION_FAIL);
        }
    }

}